 package io.geely.push.modules.job.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import io.geely.push.common.utils.Constant;
import io.geely.push.modules.push.entity.PushdataEntity;
import io.geely.push.modules.push.service.PushdataService;
import io.geely.push.modules.sys.entity.SysUserEntity;

/**
 * 
 * @author edd
 *
 */
@Component("getPushDateTask")
public class GetPushDateTask {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	PushdataService pushdataService;

	/**
	 * 获取最新mes当天的车间门禁数据 并插入到数据库
	 */
	public void getMesPushDate() {

		RestTemplate restTemplate = new RestTemplate();
		JSONObject json = restTemplate.getForEntity(Constant.GET_URL_PUSH_DATA, JSONObject.class).getBody();
		System.out.println(json);
		JSONArray jsonArray = json.getJSONArray("list");

		JSONArray parseArray = JSON.parseArray(jsonArray.toJSONString());
		// 遍历方式
		PushdataEntity entity = new PushdataEntity();
		for (Object obj : parseArray) {
			JSONObject jsonObject = (JSONObject) obj;
			PushdataEntity pushdataEntity = pushdataService
					.selectOne(new EntityWrapper<PushdataEntity>().eq("monum", jsonObject.getString("monum")));

			if (pushdataEntity != null) {
				entity.setPsdate(jsonObject.getString("psdate"));
				pushdataService.update(entity,
						new EntityWrapper<PushdataEntity>().eq("monum", jsonObject.getString("monum")));
				logger.info("数据被更新:" + entity);
			} else {
				entity.setMonum(jsonObject.getString("monum"));
				entity.setPrjcode(jsonObject.getString("prjcode"));
				entity.setPlcode(jsonObject.getString("plcode"));
				entity.setPsdate(jsonObject.getString("psdate"));
				entity.setPfdate(jsonObject.getString("pfdate"));
				pushdataService.insert(entity);
				logger.info("数据被插入:" + entity);
			}

		}

	}

	/**
	 * 发送门禁数据
	 */
	public void sendMesPushDate() {

	}

}
